.PHONY: kernel, boot1, boot2, interrupts, ddriver, clean

############ DO ## NOT ## EDIT ################

CXX=g++
CXXFLAGS= -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector -Wno-write-strings
LDFLAGS= --oformat=binary
ASM=nasm

###############################################

CPP_FILES := $(filter-out src/diskdriver.cpp, $(wildcard src/*.cpp))
OBJ_FILES := $(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o)))

# Note : the CROSS variable is now deprecated.
# Please add your own cross compiler to the path.

all: boot1 boot2 ddriver interrupts kernel
	dd if=/dev/zero of=hdd.img bs=1024 count=10080 &> /dev/null
	dd if=build/boot1 of=hdd.img conv=notrunc &> /dev/null
	dd if=build/boot2 of=hdd.img conv=notrunc bs=512 count=10 seek=1 &> /dev/null
	dd if=build/ddriver of=hdd.img bs=512 count=2 seek=11 conv=notrunc &> /dev/null
	dd if=build/kernel of=hdd.img bs=512 count=64 seek=13 conv=notrunc &> /dev/null
	

kernel: $(OBJ_FILES) interrupts
	$(CROSS)$(CROSSPREFIX)ld $(OBJ_FILES) obj/interrupts.o -o build/kernel -Ttext=0x100000 $(LDFLAGS)
	$(CROSS)$(CROSSPREFIX)ld obj/ddriver.o -o build/ddriver -Ttext=0x8000 $(LDFLAGS)

boot1:
	$(ASM) src/bootloader1.asm -f bin -o build/boot1

boot2:
	$(ASM) src/bootloader2.asm -f bin -o build/boot2

interrupts:
	$(ASM) src/interrupts.asm -f elf -o obj/interrupts.o

ddriver:
	$(CROSS)$(CROSSPREFIX)$(CXX) $(CXXFLAGS) -c src/diskdriver.cpp -o obj/ddriver.o

clean:
	rm -rf obj/*.o build/* hdd.img tmp/* bochsout.txt

obj/%.o: src/%.cpp
	$(CROSS)$(CROSSPREFIX)$(CXX) $(CXXFLAGS) -c -o $@ $<